function addtocart(id,productname,location_id)
{
	var storage = window.localStorage;
	var items = localStorage.getItem('bakery_items_'+location_id);
	if(items != null)
	{
		items = JSON.parse(items);
		var item = items['item_' + id];
		if(item != null && item != "")
		{
			item.quantity++;
			items['item_' + id] = item;
		}else
		{
			item = {};
			item.quantity = 1;
			item.itemid = id;
			items['item_' + id] = item;
		}
	}else
	{
		items = {};
		var item = {};
		item.quantity = 1;
		item.itemid = id;
		items['item_' + id] = item;
	}
	
	var quantity = 0;
	jQuery.each(items,function(key,value){
		quantity += value.quantity;
	})
	jQuery('#bakery_cart_module .total_products').html('('+quantity + ' ' + Joomla.JText._("COM_BAKERYS_ITEM_PRODUCTNUMBERJS") + ')');
	
	storage.setItem('bakery_items_'+location_id,JSON.stringify(items));
	
	var div = jQuery("<div class='modal hide fade' role='dialog'></div>");
	var html = '<div class="modal-header">';
		html += '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>';
		html += '</div>';
		html += '<div class="modal-body"><p>'+productname+' '+ Joomla.JText._("COM_BAKERYS_ITEM_ADDTOCARTJS") +'</p></div>';
		html += '<div class="modal-footer">';
		html += '<button class="btn" data-dismiss="modal" aria-hidden="true">'+Joomla.JText._("COM_BAKERYS_ITEM_CONTINUESHOPPINGJS")+'</button>';
		html += '<button class="btn btn-primary">'+Joomla.JText._("COM_BAKERYS_ITEM_SHOWCARTJS")+'</button>';
		html += '</div>';
	div.append(html);	
	div.modal();
	div.on("hidden",function(){
		div.remove();
	})
}

function removecart(id, location_id)
{
	var storage = window.localStorage;
	var items = storage.getItem('bakery_items_'+location_id);
	var tmp = {};
	if(items != null)
	{
		items=JSON.parse(items);
		delete items['item_'+id];

		storage.setItem('bakery_items_'+location_id,JSON.stringify(items));
		var hiddenfield = "<input type='hidden' name='cart' value='"+JSON.stringify(items)+"' />";
		var hiddenfieldaction = "<input type='hidden' name='action' value='delete' />";
		jQuery("#cart_form").append(hiddenfield).append(hiddenfieldaction).trigger("submit");	
	}
}

function updatecart(location_id)
{
	var tmp = {};
	jQuery("#table_cart tbody tr").each(function(){
		var productid = jQuery(this).find(".product_quantity").attr("productid");
		var quantity = jQuery(this).find(".product_quantity").val();
		var item = {};
		item.itemid = productid;
		item.quantity = quantity;
		tmp['item_'+productid] = item;
	})
	
	var storage = window.localStorage;
	storage.setItem('bakery_items_'+location_id,JSON.stringify(tmp));
	
	var hiddenfield = "<input type='hidden' name='cart' value='"+JSON.stringify(tmp)+"' />";
	jQuery("#checkout_form").append(hiddenfield).trigger("submit");	
}

